/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.controller;

import com.bjboot.caocao.common.utils.Result;
import com.bjboot.caocao.system.pojo.UserOnlinePojo;
import com.bjboot.caocao.system.service.SessionService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

/**
 *@Description 系统session控制器管理
 *@Author 操文健
 *@Date 2018-09-19 下午7:51
 *@Version 1.0
 **/
@RequestMapping("/sys/online")
@Controller
public class SessionController {

    private static final Logger logger = LoggerFactory.getLogger(SessionController.class);

    @Autowired
    SessionService sessionService;

    @ApiOperation(value = "在线用户Module")
    @GetMapping()
    public String online() {
        return "system/online/online";
    }

    @ApiOperation(value = "查询在线用户")
    @ResponseBody
    @RequestMapping("/list")
    public List<UserOnlinePojo> list() {
        return sessionService.list();
    }

    @ApiOperation(value = "强制下线用户")
    @RequiresPermissions("sys:online:offline")
    @ResponseBody
    @RequestMapping("/forceLogout/{sessionId}")
    public Result forceLogout(@PathVariable("sessionId") String sessionId, RedirectAttributes redirectAttributes) {
        try {
            sessionService.forceLogout(sessionId);
            return Result.ok();
        } catch (Exception e) {
            logger.error("强制下线用户异常", e);
            return Result.error("强制下线用户异常");
        }

    }

    @ApiOperation(value = "查询在线用户")
    @ResponseBody
    @RequestMapping("/sessionList")
    public Collection<Session> sessionList() {
        return sessionService.sessionList();
    }
}
